﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.BL;
using com.vs.Inventory.BL;
using com.vs.Task.BL;

namespace com.vs.Receiving.BL
{
    public class ReceivingHelper
    {
        public string CheckIn(Session sess, ReceiptContainerDO rcDO, ReceivingPreferenceDO prefDO)
        {
            string stErr = null;
            InventoryChangeDS icDS = null;
            InventoryHelper ivMgr = null;
            ReceiptDetailDO rdDO = null;
            int iAttributeNum = 0;
            TransactionHistoryDO thDO = null;
            Dictionary<string, object> dcValue = null;
            int iIntRecContNum = 0;
            AttributeManager atrMgr = null;
            string stRecvDock = null;
            ReceiptManager rcpMgr = null;
            string stContID = null;            

            try
            {
                #region 验证收货
                rcpMgr = new ReceiptManager();
                if (StringManager.IsEmpty(rcDO.FROM_LOCATION))
                {
                    stRecvDock = rcpMgr.GetReceivingDock(sess, rcDO.INTERNAL_RECEIPT_NUM);
                    if (StringManager.IsEmpty(stRecvDock))
                        return MessageManager.MSG_RCP_018;//错误：无法确认入库单的收货月台
                    else
                        rcDO.FROM_LOCATION = stRecvDock;
                }

                stErr = this.ValidateCheckIn(sess, rcDO, prefDO);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                #endregion

                #region 检查属性是否已存在
                atrMgr = new AttributeManager();
                iAttributeNum = atrMgr.GetAttributeNum(sess, rcDO.ITEM, rcDO.COMPANY, rcDO.ATTRIBUTE1, rcDO.ATTRIBUTE2, rcDO.ATTRIBUTE3
                    , rcDO.ATTRIBUTE4, rcDO.ATTRIBUTE5, rcDO.ATTRIBUTE6, rcDO.ATTRIBUTE7, rcDO.ATTRIBUTE8);
                rcDO.ATTRIBUTE_NUM = iAttributeNum;
                #endregion

                #region 创建新的收货托盘
                //如果系统分配托盘号，生成托盘号
                bool bFailToGenID = false;
                if (StringManager.Equals(prefDO.AUTO_ASSIGN_LPN, "Y"))
                {
                    stContID = NextNumberManager.GetNextNumber(sess, NextNumberConstants.stRECV_CONT_ID);
                    //生成托盘号失败，后面会使用主键自增值替代
                    if (StringManager.IsEmpty(stContID))
                        bFailToGenID = true;
                    else
                        rcDO.CONTAINER_ID = stContID;
                }

                stErr = rcDO.Insert(sess);
                if (StringManager.IsErrorMessage(stErr))
                    return stErr;

                iIntRecContNum =  Convert.ToInt32(stErr);
                rcDO.INTERNAL_CONTAINER_NUM = iIntRecContNum;

                //如果系统生成托盘号失败，使用主键自增值代替
                if (bFailToGenID)
                {
                    dcValue = new Dictionary<string, object>();
                    dcValue.Add("CONTAINER_ID", stErr);
                    rcDO.Update(sess, dcValue);
                    rcDO.CONTAINER_ID = stErr;
                }
                #endregion

                #region 收货，产生库存
                icDS = new InventoryChangeDS();
                icDS.WAREHOUSE = rcDO.WAREHOUSE;
                icDS.COMPANY = rcDO.COMPANY;
                icDS.ITEM = rcDO.ITEM;
                icDS.ATTRIBUTE_NUM = rcDO.ATTRIBUTE_NUM;
                icDS.LOCATION = rcDO.FROM_LOCATION;
                icDS.LPN = rcDO.CONTAINER_ID;
                //icDS.ParentLPN = ivDS.ParentLPN;
                icDS.QUANTITY = rcDO.QUANTITY;
                icDS.QUANTITY_UM = rcDO.QUANTITY_UM;
                icDS.OP_ONHANDQTY = "+";
                icDS.INVENTORY_STS = rcDO.INVENTORY_STS;

                ivMgr = new InventoryHelper();
                stErr = ivMgr.AdjustInventory(sess, icDS);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                #endregion

                #region 更新入库单状态
                stErr = rcpMgr.UpdateReceiptStatus(sess, rcDO.INTERNAL_RECEIPT_NUM);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                #endregion

                #region 记录库存交易记录
                thDO = new TransactionHistoryDO();
                thDO.AFTER_ON_HAND_QTY = 0;
                thDO.AFTER_STS = icDS.INVENTORY_STS;
                thDO.ATTRIBUTE_NUM = iAttributeNum;
                thDO.BEFORE_ON_HAND_QTY = 0;
                thDO.BEFORE_STS = icDS.INVENTORY_STS;
                thDO.COMPANY = rcDO.COMPANY;
                //thDO.DATE_TIME_STAMP = System.DateTime.Now;
                thDO.DIRECTION = Constants.stIN;
                thDO.ITEM = rcDO.ITEM;
                thDO.ITEM_DESC = rcDO.ITEM_DESC;
                thDO.LOCATION = rcDO.FROM_LOCATION;
                thDO.LPN = rcDO.CONTAINER_ID;
                thDO.QUANTITY = rcDO.QUANTITY;
                thDO.QUANTITY_UM = rcDO.QUANTITY_UM;
                thDO.REFERENCE_ID = rcDO.RECEIPT_ID;
                thDO.REFERENCE_LINE_NUM = rcDO.INTERNAL_RECEIPT_LINE_NUM;
                thDO.REFERENCE_NUM = rcDO.INTERNAL_RECEIPT_NUM;
                thDO.REFERENCE_NUM_TYPE = Constants.stRECEIPT;
                thDO.TRANSACTION_TYPE = Constants.stCHECKIN;
                thDO.USER_STAMP = rcDO.USER_STAMP;
                thDO.WAREHOUSE = rcDO.WAREHOUSE;

                thDO.Insert(sess);
                #endregion

                #region 更新入库单行剩余数量
                rdDO = ReceiptDetailDO.GetModelByPK(sess, rcDO.INTERNAL_RECEIPT_LINE_NUM);
                if (rdDO == null)
                    return MessageManager.MSG_RCP_002;
                rdDO.OPEN_QTY -= rcDO.QUANTITY;
                stErr = rdDO.Update(sess);
                #endregion

                #region 更新收货单行和收货单状态
                if (rdDO.OPEN_QTY <= 0)
                    stErr = rcpMgr.UpdateReceiptStatus(sess, rcDO.INTERNAL_RECEIPT_NUM);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                #endregion

                #region 如果自动定位，定位
                if (StringManager.Equals(prefDO.AUTO_LOCATE, "Y"))
                {
                    stErr = this.LocateContainers(sess, new int[]{iIntRecContNum}, prefDO);
                    if (!StringManager.IsEmpty(stErr))
                        return stErr;
                }
                #endregion

                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string CheckInAll(Session sess, int iIntRecNum)
        {
            List<ReceiptDetailDO> ltRcpDtlDO = null;
            string stErr = null;
            InventoryDS ivDS = null;
            ReceiptManager rcpMgr = null;
            string stRecvDock = null;

            try
            {
                ltRcpDtlDO = ReceiptDetailDO.GetListByColumn(sess, "INTERNAL_RECEIPT_NUM", iIntRecNum);
                if (null == ltRcpDtlDO || ltRcpDtlDO.Count < 1)
                    return MessageManager.MSG_RCP_001;//入库单没明细

                rcpMgr = new ReceiptManager();
                stRecvDock = rcpMgr.GetReceivingDock(sess, iIntRecNum);
                if (StringManager.IsEmpty(stRecvDock))
                    return MessageManager.MSG_RCP_018; //不能确定收货月台

                foreach (ReceiptDetailDO rdDO in ltRcpDtlDO)
                {
                    ivDS = new InventoryDS();
                    ivDS.WAREHOUSE = rdDO.WAREHOUSE;
                    ivDS.COMPANY = rdDO.COMPANY;
                    ivDS.ITEM = rdDO.ITEM;
                    ivDS.LOCATION = stRecvDock;
                    //todo:完成全部收货的逻辑
                    //stErr = this.CheckIn(sess, rdDO.INTERNAL_RECEIPT_LINE_NUM, ivDS);
                    if (!StringManager.IsEmpty(stErr))
                    {
                        HistoryManager.SaveProcessHistory(sess,
                            HistoryManager.ProcessType.iCheckIn,
                            HistoryManager.Condition.iFailed,
                            stErr,
                            rdDO.RECEIPT_ID, rdDO.ITEM, null, null);
                        continue;
                    }
                }
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
        }

        public string CloseReceipt(Session sess, int iRcpNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, "RCP_CloseReceipt", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_RECEIPT_NUM", iRcpNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string ValidateCheckIn(Session sess, int iIntRecLnNum, int iQty)
        {
            return null;
        }

        public string ValidateCheckIn(Session sess, ReceiptContainerDO rcDO, ReceivingPreferenceDO prefDO)
        {
            DataHelper helper = null;
            string stError = null;

            try
            {
                helper = new DataHelper(sess);

                //如果不是自动生成托盘号，检查托盘号是否可用
                if (!StringManager.Equals(prefDO.AUTO_ASSIGN_LPN, "Y"))
                {
                    //托盘号不能为空
                    if (StringManager.IsEmpty(rcDO.CONTAINER_ID))
                        return MessageManager.MSG_RCP_016; //错误：托盘号不能为空

                    stError = helper.GetString(CommandType.StoredProcedure, "RCP_ValidateLPN", new IDataParameter[]{
                                DataHelper.BuildParameter(sess, "@WAREHOUSE", rcDO.WAREHOUSE),
                                DataHelper.BuildParameter(sess, "@CONTAINER_ID", rcDO.CONTAINER_ID)});

                    if (!StringManager.IsEmpty(stError))
                        return stError;
                }
                return null; 
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message ;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string CancelCheckIn(Session sess, int iRecContNum)
        {
            ReceiptContainerDO rcDO = null;
            InventoryChangeDS invChgDS = null;
            InventoryHelper invMgr = null;
            string stError = null;
            string stSQL = null;
            DataHelper helper = null;
            ReceiptManager rhMgr = null;
            TransactionHistoryDO thDO = null;

            try
            {
                rcDO = ReceiptContainerDO.GetModelByPK(sess, iRecContNum);
                if (rcDO == null)
                    return MessageManager.MSG_RCP_006;//错误：收货托盘不存在

                if (rcDO.STATUS != (int)Constants.InboundStatus.iLOCATE_PENDING)
                    return MessageManager.MSG_RCP_026; //托盘目前状态无法取消收货";

                //回滚收货月台库存
                invChgDS = new InventoryChangeDS();
                invChgDS.ATTRIBUTE_NUM = rcDO.ATTRIBUTE_NUM;
                invChgDS.COMPANY = rcDO.COMPANY;
                invChgDS.INVENTORY_STS = rcDO.INVENTORY_STS;
                invChgDS.ITEM = rcDO.ITEM;
                invChgDS.LOCATION = rcDO.FROM_LOCATION;
                invChgDS.LPN = rcDO.CONTAINER_ID;
                invChgDS.OP_ALLOCQTY = "";
                invChgDS.OP_INTRANQTY = "";
                invChgDS.OP_ONHANDQTY = "-";
                invChgDS.QUANTITY = rcDO.QUANTITY;
                invChgDS.QUANTITY_UM = rcDO.QUANTITY_UM;
                invChgDS.WAREHOUSE = rcDO.WAREHOUSE;

                invMgr = new InventoryHelper();
                stError = invMgr.AdjustInventory(sess, invChgDS);
                if (!StringManager.IsEmpty(stError))
                    return stError;

                //恢复收货单明细数量
                helper = new DataHelper(sess);
                stSQL = " UPDATE RECEIPT_DETAIL SET OPEN_QTY = OPEN_QTY + " + rcDO.QUANTITY + " WHERE INTERNAL_RECEIPT_LINE_NUM = " + rcDO.INTERNAL_RECEIPT_LINE_NUM;
                helper.Update(CommandType.Text, stSQL);

                //删除收货托盘
                rcDO.Delete(sess);

                //记录取消收货历史记录
                thDO = new TransactionHistoryDO();
                thDO.AFTER_ON_HAND_QTY = 0;
                thDO.AFTER_STS = invChgDS.INVENTORY_STS;
                thDO.ATTRIBUTE_NUM = invChgDS.ATTRIBUTE_NUM;
                thDO.BEFORE_ON_HAND_QTY = 0;
                thDO.BEFORE_STS = invChgDS.INVENTORY_STS;
                thDO.COMPANY = rcDO.COMPANY;
                //thDO.DATE_TIME_STAMP = System.DateTime.Now;
                thDO.DIRECTION = Constants.stOUT;
                thDO.ITEM = rcDO.ITEM;
                thDO.ITEM_DESC = rcDO.ITEM_DESC;
                thDO.LOCATION = rcDO.FROM_LOCATION;
                thDO.LPN = rcDO.CONTAINER_ID;
                thDO.QUANTITY = rcDO.QUANTITY;
                thDO.QUANTITY_UM = rcDO.QUANTITY_UM;
                thDO.REFERENCE_ID = rcDO.RECEIPT_ID;
                thDO.REFERENCE_LINE_NUM = rcDO.INTERNAL_RECEIPT_LINE_NUM;
                thDO.REFERENCE_NUM = rcDO.INTERNAL_RECEIPT_NUM;
                thDO.REFERENCE_NUM_TYPE = Constants.stRECEIPT;
                thDO.TRANSACTION_TYPE = Constants.stCANCELCHECKIN;
                thDO.USER_STAMP = rcDO.USER_STAMP;
                thDO.WAREHOUSE = rcDO.WAREHOUSE;

                thDO.Insert(sess);

                //更新入库单状态
                rhMgr = new ReceiptManager();
                stError = rhMgr.UpdateReceiptStatus(sess, rcDO.INTERNAL_RECEIPT_NUM);

                return stError;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string LocateContainers(Session sess, int[] arRecContNum, ReceivingPreferenceDO prefDO)
        {
            int iWaveNum = 0;
            string stErr = null;
            StringBuilder sbErr = null;
            TaskGenerationHelper taskMgr = null;
            //Dictionary<string, object> dcData = null;
            LocatingHelper helper = null;
            ReceiptContainerDO rcDO = null;
            List<InventoryTransDS> lsIT = null;
            ReceiptContManager rctMgr = null;
            ReceiptManager rcpMgr = null;

            try
            {
                #region 获取处理批次（多个货箱同时定位作为批次处理）
                stErr = NextNumberManager.GetNextNumber(sess, NextNumberConstants.stLOCATE_WAVE_NUM);
                if (StringManager.IsEmpty(stErr))
                {
                    return MessageManager.MSG_RCP_022;//错误：无法生成收货波次序号";
                }

                iWaveNum = Convert.ToInt32(stErr);
                #endregion

                #region 定位
                sbErr = new StringBuilder();
                helper = new LocatingHelper();
                helper.bCreateTask = StringManager.Equals(prefDO.AUTO_PUTAWAY, "Y") ? false : true;

                foreach (int iRecContNum in arRecContNum)
                {
                    stErr = null;

                    rcDO = ReceiptContainerDO.GetModelByPK(sess, iRecContNum);
                    if (null == rcDO)
                    {
                        sbErr.AppendLine(MessageManager.MSG_RCP_006 + "：" + iRecContNum);//收货货箱不存在
                        continue;
                    }

                    //检查状态
                    if (rcDO.STATUS != (int)Constants.InboundStatus.iLOCATE_PENDING)
                    {
                        sbErr.AppendLine(MessageManager.MSG_RCP_023 + "：" + rcDO.CONTAINER_ID);//托盘目前状态无法定位";
                        continue;
                    }

                    //开始定位
                    lsIT = helper.LocateReceiptContainer(sess, rcDO, iWaveNum, ref stErr);

                    if (!StringManager.IsEmpty(stErr))
                    {
                        sbErr.AppendLine(stErr +　"：" + rcDO.CONTAINER_ID);
                        continue;
                    }

                    if (null == lsIT || lsIT.Count < 1)
                    {
                        sbErr.AppendLine(MessageManager.MSG_RCP_007 + "：" + rcDO.CONTAINER_ID);//定位失败
                        continue;
                    }
                }
                #endregion

                #region 更新入库单状态
                rcpMgr = new ReceiptManager();
                stErr = rcpMgr.UpdateReceiptStatus(sess, rcDO.INTERNAL_RECEIPT_NUM);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                #endregion

                #region 如果不是自动上架，创建上架任务；否则直接关闭入库货箱
                rctMgr = new ReceiptContManager();
                if (!StringManager.Equals(prefDO.AUTO_PUTAWAY, "Y"))
                {
                    taskMgr = new TaskGenerationHelper();
                    //创建任务
                    stErr = taskMgr.CreateWaveTasksByMaster(sess, iWaveNum, TaskConstants.RECV_PUTAWAY_TASK);
                    if (!StringManager.IsEmpty(stErr))
                    {
                        return stErr;
                    }
                    //更新托盘状态
                    stErr = rctMgr.UpdateContainerStatusByWave(sess, iWaveNum, (int)Constants.InboundStatus.iPUTAWAY_PENDING);
                    if (!StringManager.IsEmpty(stErr))
                    {
                        return stErr;
                    }
                }
                //如果不需要创建上架任务，直接更新托盘状态为900
                else
                {
                    stErr = rctMgr.UpdateContainerStatusByWave(sess, iWaveNum, (int)Constants.InboundStatus.iCLOSED);
                    if (!StringManager.IsEmpty(stErr))
                    {
                        return stErr;
                    }
                }
                #endregion

                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        public string Unlocate(Session sess, int iIntRecContNum)
        {
            string stError = null;
            DataHelper helper = null;
            ReceiptContainerDO rcDO = null;
            InventoryChangeDS invChgDS = null;
            List<InventoryChangeDS> arInvChg = null;
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            Dictionary<string, object> dcValue = null;
            DataTable dtTask = null;
            InventoryHelper invHelper = null;
            InventoryManager invMgr = null;
            ReceiptManager rcpMgr = null;
            List<LocationInventoryDO> ltInv = null;

            try
            {
                #region 验证是否可以取消
                /*
                stError = helper.GetString(CommandType.StoredProcedure, "RCP_ValidateUnlocate", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iIntRecContNum)});

                if (!StringManager.IsEmpty(stError))
                    return stError;
                */
                rcDO = ReceiptContainerDO.GetModelByPK(sess, iIntRecContNum);
                if (rcDO == null)
                    return MessageManager.MSG_RCP_006; //错误：托盘不存在

                //如果尚未定位
                //如果创建上架任务，且任务已被执行
                //如果不创建任务，且目标库位库存已不足以取消
                //返回错误
                if (rcDO.STATUS < (int)Constants.InboundStatus.iPUTAWAY_PENDING)
                    return MessageManager.MSG_RCP_024; //托盘目前状态无法取消定位

                if (rcDO.TASK_CREATED == "Y" && 
                    rcDO.STATUS > (int)Constants.InboundStatus.iPUTAWAY_PENDING)
                    return MessageManager.MSG_RCP_024; //托盘目前状态无法取消定位

                //如果不创建任务，检查目标库位是否有足够库存来取消
                //判断目标库位是否track LPN
                bool bTrackLPN = LocationManager.DoseLocTrackLPN(sess, rcDO.WAREHOUSE, rcDO.TO_LOCATION);
                //if (rcDO.TASK_CREATED == "N")
                //{
                invMgr = new InventoryManager();
                ltInv = invMgr.GetInventory(sess, rcDO.WAREHOUSE, rcDO.COMPANY, rcDO.ITEM, rcDO.TO_LOCATION, bTrackLPN ? rcDO.CONTAINER_ID : null, rcDO.ATTRIBUTE_NUM, rcDO.INVENTORY_STS);
                if (null == ltInv || ltInv.Count < 1)
                    return MessageManager.MSG_RCP_025; //目的库位没有足够库存完成取消";
                
                LocationInventoryDO invDO = ltInv[0];
                int iAvailableQty = 0;

                if (StringManager.Equals(rcDO.TASK_CREATED, "Y"))
                    iAvailableQty = invDO.IN_TRANSIT_QTY;
                else
                    iAvailableQty = invDO.ON_HAND_QTY + invDO.IN_TRANSIT_QTY - invDO.ALLOCATED_QTY;
                if (iAvailableQty < rcDO.QUANTITY)
                    return MessageManager.MSG_RCP_025; //目的库位没有足够库存完成取消";
                //}
                #endregion

                #region 回滚库存
                arInvChg = new List<InventoryChangeDS>();
                //扣除目标库位库存
                invChgDS = new InventoryChangeDS();
                invChgDS.ATTRIBUTE_NUM = rcDO.ATTRIBUTE_NUM;
                invChgDS.COMPANY = rcDO.COMPANY;
                invChgDS.INVENTORY_STS = rcDO.INVENTORY_STS;
                invChgDS.ITEM = rcDO.ITEM;
                invChgDS.LOCATION = rcDO.TO_LOCATION;
                invChgDS.LPN = bTrackLPN ? rcDO.CONTAINER_ID : null;
                if (StringManager.Equals(rcDO.TASK_CREATED, "Y"))
                {
                    invChgDS.OP_ALLOCQTY = "";
                    invChgDS.OP_INTRANQTY = "-";
                    invChgDS.OP_ONHANDQTY = "";
                }
                else
                {
                    invChgDS.OP_ALLOCQTY = "";
                    invChgDS.OP_INTRANQTY = "";
                    invChgDS.OP_ONHANDQTY = "-";
                }
                invChgDS.QUANTITY = rcDO.QUANTITY;
                invChgDS.QUANTITY_UM = rcDO.QUANTITY_UM;
                invChgDS.WAREHOUSE = rcDO.WAREHOUSE;

                arInvChg.Add(invChgDS);

                //恢复原库位库存
                invChgDS = new InventoryChangeDS();
                invChgDS.ATTRIBUTE_NUM = rcDO.ATTRIBUTE_NUM;
                invChgDS.COMPANY = rcDO.COMPANY;
                invChgDS.INVENTORY_STS = rcDO.INVENTORY_STS;
                invChgDS.ITEM = rcDO.ITEM;
                invChgDS.LOCATION = rcDO.FROM_LOCATION;
                invChgDS.LPN = rcDO.CONTAINER_ID;
                if (StringManager.Equals(rcDO.TASK_CREATED, "Y"))
                {
                    invChgDS.OP_ALLOCQTY = "-";
                    invChgDS.OP_INTRANQTY = "";
                    invChgDS.OP_ONHANDQTY = "";
                }
                else
                {
                    invChgDS.OP_ALLOCQTY = "";
                    invChgDS.OP_INTRANQTY = "";
                    invChgDS.OP_ONHANDQTY = "+";
                }
                invChgDS.QUANTITY = rcDO.QUANTITY;
                invChgDS.QUANTITY_UM = rcDO.QUANTITY_UM;
                invChgDS.WAREHOUSE = rcDO.WAREHOUSE;

                arInvChg.Add(invChgDS);


                invHelper = new InventoryHelper();
                stError = invHelper.AdjustInventory(sess, arInvChg);
                if (!StringManager.IsEmpty(stError))
                    return stError;
                #endregion

                #region 删除任务
                helper = new DataHelper(sess);
                stError = helper.GetString(CommandType.StoredProcedure, "TSK_DTaskByContainer", new IDataParameter[]{
                                DataHelper.BuildParameter(sess, "@REFERENCE_CONT_NUM", rcDO.INTERNAL_CONTAINER_NUM),                                
                                DataHelper.BuildParameter(sess, "@INTERNAL_NUM_TYPE", TaskConstants.RECEIPT)
                            });
                if (!StringManager.IsEmpty(stError))
                    return stError;
                #endregion

                #region 更新收货托盘的库位为空，更新状态
                dcValue = new Dictionary<string, object>();
                dcValue.Add("TO_LOCATION", null);
                dcValue.Add("STATUS", Constants.InboundStatus.iLOCATE_PENDING);
                rcDO.Update(sess, dcValue);
                #endregion

                #region 更新入库单状态
                rcpMgr = new ReceiptManager();
                stError = rcpMgr.UpdateReceiptStatus(sess, rcDO.INTERNAL_RECEIPT_NUM);
                if (!StringManager.IsEmpty(stError))
                    return stError;
                #endregion

                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
